{% extends '../_manage.html' %}

{% block title %} {{ form.name }} {% endblock %}

{% block head %}
<script>
$(function () {
    var id = '{{ id }}';
    if (id) {
        getJSON('/api/wikis/' + id, function (err, wiki) {
            if (err) {
                return showError(err);
            }
            initVM(wiki);
        });
    }
    else {
        initVM({
            id: ''
        });
    }
});

function initVM(wiki) {
    if (wiki.cover_id) {
        $('#image-preview').css('background-image', 'url(/files/attachments/' + wiki.cover_id + '/l)');
    }
    var vm = new Vue({
        el: '#vm',
        data: {
            image: null,
            id: wiki.id,
            cover_id: wiki.cover_id,
            name: wiki.name,
            tag: wiki.tag,
            description: wiki.description,
            content: wiki.content || ''
        },
        created: function () {
            $('#loading').hide();
            $('#vm').show();
        },
        methods: {
            getPostData: function () {
                var data = {
                    name: this.name,
                    description: this.description,
                    tag: this.tag
                }
                var content = $('#content-editor').val();
                if (this.content !== content) {
                    data.content = content;
                }
                if (this.image !== null) {
                    data.image = this.image;
                }
                return data;
            },

            submit: function() {
                var data;
                try {
                    data = this.getPostData();
                }
                catch (e) {
                    onJsonFormError(e);
                    return;
                }
                var that = this;
                clearFormError();
                that.$resource('{{ form.action }}').save(data).then(function (resp) {
                    resp.json().then(function (result) {
                        that.saved = true;
                        location.assign('{{ form.redirect }}');
                    });
                }, onJsonFormError);
            }
        }
    });

    // check content when unload:
    window.onbeforeunload = function() {
        if (vm.saved) {
            return;
        }
        if (vm.content !== $('#content-editor').val()) {
            return 'Content has been changed.';
        }
    };

    $('#content-editor').val(wiki.content || '');
    var htmleditor = UIkit.htmleditor($('#content-editor').get(0), {
        markdown: true,
        maxsplitsize: 600
    });

    // watch file change:
    $('#file').change(function (evt) {
        evt.preventDefault();
        var f = $('#file').val();
        if (f === '') {
            // user cancelled selected file:
            vm.image = null;
            $('#image-preview').css('background-image', 'none');
            return;
        }
        try {
            var file = $('#file').get(0).files[0];
            processImageFile(file, function (r) {
                vm.image = r.data;
                $('#image-preview').css('background-image', 'url(' + r.image + ')');
            });
        }
        catch (e) {
            UIkit.modal.alert('Error when process file: ' + e);
        }
    });
}
</script>

{% endblock %}

{% block main %}

    <div id="loading" class="uk-width-1-1">
        <i class="uk-icon-spinner uk-icon-spin"></i> {{ _('Loading') }}...
    </div>

    <div id="vm" class="uk-width-1-1">
        <ul class="uk-breadcrumb">
            <li><a href="wiki_list">{{ _('All Wikis') }}</a></li>
            <li class="uk-active"><span>{{ form.name }}</span></li>
        </ul>

        <form v-on:submit.prevent="submit" class="uk-form uk-form-stacked uk-margin">
            <legend>{{ form.name }}</legend>
            <fieldset>
                <div class="uk-alert uk-alert-danger uk-hidden"></div>
                <div class="uk-form-row">
                    <label class="uk-form-label">{{ _('Image') }}:</label>
                    <div class="uk-form-controls">
                        <div id="image-preview" style="border: 1px solid #ccc; width:642px; height:362px; background-size:cover;"></div>
                    </div>
                    <div class="uk-form-controls">
                        <input id="file" type="file">
                    </div>
                </div>
                <div class="uk-form-row">
                    <label class="uk-form-label">{{ _('Name') }}:</label>
                    <div class="uk-form-controls">
                        <input v-model="name" name="name" type="text" maxlength="100" class="uk-width-1-1" placeholder="{{ _('Name') }}">
                    </div>
                </div>
                <div class="uk-form-row">
                    <label class="control-label">{{ _('Tags') }}:</label>
                    <div class="controls">
                        <input v-model="tag" name="tag" type="text" class="uk-width-1-1" placeholder="tags seperated by ,">
                    </div>
                </div>
                <div class="uk-form-row">
                    <label class="uk-form-label">{{ _('Description') }}:</label>
                    <div class="uk-form-controls">
                        <textarea v-model="description" name="description" rows="6" class="uk-width-1-1" style="resize:none;"></textarea>
                    </div>
                </div>
                <div class="uk-form-row">
                    <label class="control-label">{{ _('Content') }}:</label>
                    <div class="controls">
                        <textarea id="content-editor" rows="20" style="width:100%; resize:none;"></textarea>
                    </div>
                </div>
                <div class="uk-form-row">
                    <div class="controls">
                        <button type="submit" class="uk-button uk-button-primary"><i class="uk-icon-check"></i> {{ _('Save') }}</button>
                        <button type="button" class="uk-button" onclick="location.assign('{{ form.redirect }}')"><i class="uk-icon-times"></i> {{ _('Cancel') }}</button>
                    </div>
                </div>
            </fieldset>
        </form>
    </div>

{% endblock %}
